package com.flowable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.util.CopyUtil;
import com.common.util.DateUtils;
import com.core.flowable.domain.SysListener;
import com.core.flowable.mapper.SysListenerMapper;
import com.flowable.Excel.SysListenerExcel;
import com.flowable.mapper.SysListenerExMapper;
import com.flowable.service.ISysListenerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 流程监听Service业务层处理
 * 
 * @author Tony
 * @date 2022-12-25
 */
@Service
public class SysListenerServiceImpl implements ISysListenerService
{
    @Autowired
    private SysListenerExMapper sysListenerExMapper;
    @Autowired
    private SysListenerMapper sysListenerMapper;


    /**
     * 查询流程监听
     * 
     * @param id 流程监听主键
     * @return 流程监听
     */
    @Override
    public ResponseEntity selectSysListenerById(Long id)
    {
        return ResponseEntity.ok(sysListenerExMapper.selectSysListenerById(id));
    }

    /**
     * 查询流程监听列表
     * 
     * @param sysListener 流程监听
     * @return 流程监听
     */
    @Override
    public ResponseEntity selectSysListenerList(Page pageDto,SysListener sysListener)
    {
        IPage<SysListener> page = sysListenerExMapper.selectSysListenerList(new Page<>(pageDto.getCurrent(), pageDto.getSize()), sysListener);
        return ResponseEntity.ok(page);
    }

    /**
     * 新增流程监听
     * 
     * @param sysListener 流程监听
     * @return 结果
     */
    @Override
    public int insertSysListener(SysListener sysListener)
    {
        sysListener.setCreateTime(DateUtils.getNowDate());
        return sysListenerExMapper.insertSysListener(sysListener);
    }

    /**
     * 修改流程监听
     * 
     * @param sysListener 流程监听
     * @return 结果
     */
    @Override
    public int updateSysListener(SysListener sysListener)
    {
        sysListener.setUpdateTime(DateUtils.getNowDate());
        return sysListenerExMapper.updateSysListener(sysListener);
    }

    @Override
    public ResponseEntity save(SysListener sysListener) {
        if(ObjectUtils.isEmpty(sysListener.getId())){
          return  ResponseEntity.ok(this.insertSysListener(sysListener));
        }else{
            return ResponseEntity.ok(this.updateSysListener(sysListener));
        }

    }

    /**
     * 批量删除流程监听
     * 
     * @param ids 需要删除的流程监听主键
     * @return 结果
     */
    @Override
    public ResponseEntity deleteSysListenerByIds(Long[] ids)
    {
        return ResponseEntity.ok(sysListenerExMapper.deleteSysListenerByIds(ids));
    }

    /**
     * 删除流程监听信息
     * 
     * @param id 流程监听主键
     * @return 结果
     */
    @Override
    public int deleteSysListenerById(Long id)
    {
        return sysListenerExMapper.deleteSysListenerById(id);
    }

    @Override
    public List<SysListenerExcel> ExportList() {
        List<SysListener> sysListeners = sysListenerMapper.selectList(new LambdaQueryWrapper<SysListener>().orderBy(true, false,SysListener::getCreateTime));
        return CopyUtil.copyList(sysListeners, SysListenerExcel.class);
    }
}
